#!/usr/bin/env Rscript

rm(list = ls())

setwd("../../data/voter_file_aggregated")

library(stargazer)

cumulative <- FALSE

## this is model output
load(paste("party_change_models_time2_glm_votes_together_coefsonly",ifelse(cumulative,"_cumulative",""),".RData",sep=""))
if (cumulative) {                       #named placebo_coefs vs coefs_placebo by accident
load(paste("party_change_models_time2_glm_votes_together_coefsonly_placebo",ifelse(cumulative,"_cumulative",""),".RData",sep=""))
} else {
load("party_change_models_time2_glm_votes_together_placebo_coefsonly.RData")
}

plotTimeStuff <- function(coefs,ses,plotName,diverge=TRUE,noAffilEtc=FALSE,axis.yes=FALSE,add=TRUE,the.pch=16,the.line.type=1,the.col="black",the.xs=1:4,the.ymax=7,the.cumulative=cumulative) {
    ##
    the.estimates <- names(coefs[[1]])
    the.ests <- grep("hid", the.estimates)
    ##
    intercept.ests <- sapply(
        coefs,
        function(x) x[1]
        )
    intercept.ses <- sapply(
        ses,
        function(x) x[1]
        )
    main.ests <- sapply(
        coefs,
        function(x) x[grep("hid",names(x))]
        )
    ##
    main.ses <- sapply(
        ses,
        function(x) x[grep("hid",names(x))]
        )
    ##
    if (diverge & !add) {
    plot(the.xs, exp(main.ests[1,]), pch=8, ylim = c(-0.3, 0.05), bty="n", xaxt="n", cex=1.25, xlim=c(-1.5,4.5), ylab="Party Divergence", main = "",xlab="",type="n")
} else if (!add) {
        plot(the.xs, exp(main.ests[1,]), pch=8, ylim=c(0,the.ymax), bty="n", xaxt="n", xlim=c(-1.5,4.5), ylab="Party Divergence", main = plotName,xlab="",cex=2,cex.lab=2,cex.main=1.8, cex.axis=2, yaxt=ifelse(axis.yes,"s","n"),type="n")
}
    abline(h = 1, lty = 2, col="red")
    abline(v = c(1.5), lty = 3, col="blue", lwd = 2)
    if (diverge) {
    text(x=1.625,y=0.025,labels="separating",col="blue")
} else {
   ## text(x=1.625,y=0.25,labels="separating",col="blue")
}
    segments(the.xs, exp(main.ests[1,] - 1.96 * main.ses), the.xs, exp(main.ests[1,] + 1.96 * main.ses),lwd=2,col=the.col)
    ##
    points(the.xs, exp(main.ests[1,]), pch=the.pch, cex=ifelse(!add,2,1.75),col=the.col)
    if (the.cumulative) {
    lines(the.xs, exp(main.ests[1,]),lwd=ifelse(!add,4,2),lty=the.line.type,col=the.col)
}
}



## pdf(paste("dyad_separation",ifelse(cumulative,"_cumulative",""),"_or.pdf",sep=""),height=7,width=7)
par(mfrow=c(1,1));  par(mar=c(0,0,7,1)); par(oma = c(5,8,0,0))
plotTimeStuff(
    DYAD.partyChangeModels.Switch.time2.glm.votes.coefs,
    DYAD.partyChangeModels.Switch.time2.glm.votes.ses,
    the.xs=1:4,
    the.ymax=ifelse(cumulative, 5, 7),
    paste(ifelse(cumulative,8,2),"year model\n(control",ifelse(cumulative,"first","prior"),"period)\n"),
    diverge=FALSE,noAffilEtc=TRUE,axis.yes=TRUE,add=FALSE,the.pch=15,the.line.type=1,the.col="purple3")
plotTimeStuff(
    DYAD.partyChangeModels.Switch.time2.glm.votes.placebo.coefs,
    DYAD.partyChangeModels.Switch.time2.glm.votes.placebo.ses,
    "",diverge=FALSE,noAffilEtc=TRUE,axis.yes=TRUE,add=TRUE,the.pch=15,the.line.type=1,the.col="gray",the.xs=-1:2-0.05
    )
mtext("separated -->",side=3,col="red",cex=1.8,at=2.25,padj=-1)
mtext(paste(ifelse(cumulative,"Cumulative","Marginal"),"Party Divergence\n(odds ratio)"),side=2, padj = -1, cex = 2.25)
## mtext("Party Divergence",side=2, padj = -3, cex = 2.25)
legend("topleft",legend=c("Switch,\nseparating:","10","14"), pch=c(NA,15,15),bty="n", cex = 1.8,col=c(NA,"purple3","gray"))
    axis(side=1, at = -1:2, labels = c("08","10", "12","14"), tick=FALSE,cex.axis=1.1,col.axis=gray(0.4),padj=-4)
    axis(side=1, at = c(1,3), labels = c("08", "12"), tick=FALSE,cex.axis=1.8,col.axis="purple3")
    axis(side=1, at = c(2,4), labels = c("10", "14"), tick=FALSE,cex.axis=1.8,padj=1.5,col.axis="purple3")
mtext("(placebo)",side=1,at=-0.5,padj=2.5,cex=1.8,col="darkorange1")
text(x=3,y=1.1,labels="(comparison)",col="red",cex=1.2)
## dev.off()

## pdf(paste("dyad_separation2_or",ifelse(cumulative,"_cumulative",""),".pdf",sep=""),height=7,width=7)
par(mfrow=c(1,1));  par(mar=c(0,0,7,1)); par(oma = c(5,8,0,0))
plotTimeStuff(
    DYAD.partyChangeModels.Switch.time2.glm.votes.coefs,
    DYAD.partyChangeModels.Switch.time2.glm.votes.ses,
    the.xs=1:4,
    "",diverge=FALSE,noAffilEtc=TRUE,axis.yes=TRUE,add=FALSE,the.pch=15,the.line.type=1,the.col="purple3",
    the.ymax=7)
plotTimeStuff(
    DYAD.partyChangeModels.Switch.time2.glm.votes.placebo.coefs,
    DYAD.partyChangeModels.Switch.time2.glm.votes.placebo.ses,
    "",diverge=FALSE,noAffilEtc=TRUE,axis.yes=TRUE,add=TRUE,the.pch=15,the.line.type=1,the.col="gray",the.xs=-1:2-0.05
    )
plotTimeStuff(
    DYAD.partyChangeModels.DropAffil.time2.glm.votes.placebo.coefs,
    DYAD.partyChangeModels.DropAffil.time2.glm.votes.placebo.ses,
    "",diverge=FALSE,noAffilEtc=TRUE,axis.yes=TRUE,add=TRUE,the.pch=8,the.line.type=1,the.col="gray",the.xs=-1:2-0.05
    )
plotTimeStuff(
    DYAD.partyChangeModels.noAffil.time2.glm.votes.placebo.coefs,
    DYAD.partyChangeModels.noAffil.time2.glm.votes.placebo.ses,
    "",diverge=FALSE,noAffilEtc=TRUE,axis.yes=TRUE,add=TRUE,the.pch=17,the.line.type=1,the.col="gray",the.xs=-1:2-0.05
    )
plotTimeStuff(
    DYAD.partyChangeModels.DropAffil.time2.glm.votes.coefs,
    DYAD.partyChangeModels.DropAffil.time2.glm.votes.ses,
    the.xs=1:4-0.025,
    "Abandon",diverge=FALSE,noAffilEtc=TRUE,axis.yes=TRUE,the.pch=8,the.line.type=2
    )
mtext(ifelse(cumulative,"Cumulative Party Divergence\n(odds ratio)","Marginal Party Divergence\n(odds ratio)"),side=2, padj = -1, cex = 2.25)
    axis(side=1, at = -1:2, labels = c("06-08","08-10", "10-12","12-14"), tick=FALSE,cex.axis=1.1,col.axis=gray(0.4),padj=-4)
    axis(side=1, at = c(1,3), labels = c("06-08", "10-12"), tick=FALSE,cex.axis=1.8,col.axis="purple3")
    axis(side=1, at = c(2,4), labels = c("08-10", "12-14"), tick=FALSE,cex.axis=1.8,padj=1.5,col.axis="purple3")
text(x=3,y=1.1,labels="(comparison)",col="red",cex=1.2)
plotTimeStuff(
    DYAD.partyChangeModels.noAffil.time2.glm.votes.coefs,
    DYAD.partyChangeModels.noAffil.time2.glm.votes.ses,
    the.xs=1:4+0.05,
    "Adopt",diverge=FALSE,noAffilEtc=TRUE,axis.yes=TRUE,the.pch=17,the.line.type=2,
    the.col="purple3"
    )
## dev.off()
